package com.fhz.day19;

import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.PopupWindow;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import com.bawei.lrcutils.YaoLrcHelper;
import com.fhz.day19.adapter.MusicAdapter;
import com.fhz.day19.adapter.PopMusicAdapter;
import com.fhz.day19.model.Song;
import com.fhz.day19.utils.MusicUtils;
import com.google.gson.Gson;
import com.lauzy.freedom.library.Lrc;
import com.lauzy.freedom.library.LrcView;

import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

public class MainActivity2 extends AppCompatActivity {


    private MediaPlayer mediaPlayer;
    private LrcView lrvView;
    private ImageView next;
    private ImageView menu;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);
        initView();


        //据播放进度更新歌词:
        Timer timer = new Timer();
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                lrvView.updateTime(MusicUtils.getInstance().getCurrentPostion());
            }
        }, 0, 100);

        //周杰伦
        changeLrc();

        //  通过指示器，跳转至具体的时间:
        lrvView.setOnPlayIndicatorLineListener(new LrcView.OnPlayIndicatorLineListener() {
            @Override
            public void onPlay(long time, String content) {
                MusicUtils.getInstance().seekTo((int) time);
            }
        });

        next.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                MusicUtils.getInstance().next();
                changeLrc();
            }
        });


        menu.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //抽成方法
                openPopWindow();
            }
        });
    }


    //弹出窗口
    private void openPopWindow() {
        PopupWindow popupWindow = new PopupWindow(this);
        popupWindow.setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
        popupWindow.setHeight(600);

        //加了checkbox 以后没有发布点击了.需要设置这句话
        popupWindow.setFocusable(true);
        popupWindow.setOutsideTouchable(true);


        View inflate = LayoutInflater.from(this).inflate(R.layout.pop_layout, null);



        ListView listView = inflate.findViewById(R.id.pop_lv);

        PopMusicAdapter musicAdapter = new PopMusicAdapter(MusicUtils.getInstance().getDataList(),this);
        listView.setAdapter(musicAdapter);

        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                //播放
                MusicUtils.getInstance().play(position);
                changeLrc();
            }//周杰伦
        });

        CheckBox checkBox = inflate.findViewById(R.id.checkBox);
        checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if(isChecked){
                    //TODO  周杰伦
                    musicAdapter.setCheck(true);
                }else{
                    musicAdapter.setCheck(false);
                }
                musicAdapter.notifyDataSetChanged();
            }

        });
        popupWindow.setContentView(inflate);


        View inflate1 = LayoutInflater.from(this).inflate(R.layout.activity_main, null);

        popupWindow.showAtLocation(inflate1, Gravity.BOTTOM,0,0);
    }

    private void changeLrc() {
        //当前数据源的lrc
        int index = MusicUtils.getInstance().getIndex();
        List<Song.Data> dataList = MusicUtils.getInstance().getDataList();

        YaoLrcHelper.parseUrl(dataList.get(index).getLrc(), new YaoLrcHelper.LrcCallBack() {
            @Override
            public void getLrcData(List<Lrc> list) {
                lrvView.setLrcData(list);//邓紫棋
            }
        });
    }

    private void initView() {
        lrvView = (LrcView) findViewById(R.id.lrv_View);
        next = (ImageView) findViewById(R.id.next);
        menu = (ImageView) findViewById(R.id.menu);
    }
}